--#include "data\functions\Activity\CombineServer\CombineServerCommon.lua" once
--合服消费排行

-- 活动结束
function CombineServerConsumeRankEnd()
	if not IsInCombineServerActivityEnd() then
		return
	end
    local config = CombineServerCfg.ConsumeRank
    local rankPtr = Ranking.getRanking(RankList[RANK_COMBINESERVER_CONSUME].strName)
    if rankPtr then
        local rankCount = Ranking.getRankItemCount(rankPtr)
        for idx = 1, rankCount do
            local item = Ranking.getItemFromIndex(rankPtr, idx - 1)
            if item then
                local myScore = Ranking.getPoint(item)    
                if myScore < config.needMinValue then
                    return
                else
                    local awardConfig = GetElemByCond(config.Awards, idx)
                    if not awardConfig then
                        return
                    end
                    local actorId = tonumber(Ranking.getId(item))
                    local title = string.format(Lang.ScriptTips.CombineServerRank023, idx)
                    local content = string.format(Lang.ScriptTips.CombineServerRank024, idx)
                    local nActorJob = tonumber(Ranking.getSub(item, 2))
                    local nActorSex = tonumber(Ranking.getSub(item, 3))
                    Awards.GiveToMailById(actorId, awardConfig.awards, title, content, LogIds.CombineServerConsumeRankLogId, Lang.LogTips.CombineServerConsumeRank, nActorJob, nActorSex)
                end
            end
        end
    end
end

-- 打包数据
function CombineServerConsumeRankSendData(sysarg)
	if not IsInCombineServerActivity() then
		return
	end
    local config = CombineServerCfg.ConsumeRank
    local myIdx = 0
    local myScore = 0
    local rankPtr = Ranking.getRanking(RankList[RANK_COMBINESERVER_CONSUME].strName)
    if rankPtr then
        local actorid = Actor.getActorId(sysarg)
        local item = Ranking.getItemPtrFromId(rankPtr, actorid)
        if item then
            myIdx = Ranking.getIndexFromPtr( item ) + 1
            myScore = Ranking.getPoint(item)    
            if myScore < config.needMinValue then
                myIdx = 0
            end
        end
    end
    local npack = DataPack.allocPacket(sysarg,LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sGetCombineServerRankData)
	if not npack then return end
	DataPack.writeByte(npack, 2)
    DataPack.writeShort(npack, myIdx)
    DataPack.writeInt(npack, myScore)

    DataPack.flush(npack) 
end

function CombineServerConsumeRankConsumeYuanBao(sysarg, value)
	if not IsInCombineServerActivity() then
		return
	end
    if value <= 0 then
        return
    end
    local actorid = Actor.getActorId(sysarg)
    gRankMgr_AddValue(actorid, RANK_COMBINESERVER_CONSUME, value)
    gRankMgr_SetRankEx(actorid, RANK_COMBINESERVER_CONSUME, 2, Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION))
    gRankMgr_SetRankEx(actorid, RANK_COMBINESERVER_CONSUME, 3, Actor.getIntProperty(sysarg, PROP_ACTOR_SEX))
    CombineServerConsumeRankSendData(sysarg)
end

